package gemini;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import aries.Setting;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Font;

public class Interface {
    
    /**
     * addFocusEffect() parameter.
     */
    public static final byte BLANK_ON_FOCUS = 0;
    
    /**
     * addFocusEffect() parameter.
     */
    public static final byte BLANK_ON_TYPE = 1;
    
    /**
     * Set your Swing UI into native look.
     */
    public static void nativeLook() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            if (Setting.getShowException())
                ex.printStackTrace();
        } catch (InstantiationException ex) {
             if (Setting.getShowException())
                ex.printStackTrace();
        } catch (IllegalAccessException ex) {
             if (Setting.getShowException())
                ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
             if (Setting.getShowException())
                ex.printStackTrace();
        }
    }
    
    /**
     * Make your JFrame to the center of your display.
     * 
     * @param frame 
     */
    public static void center(JFrame frame) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension size = frame.getSize();
        int x = (int) ((screen.width - size.width) / 2);
        int y = (int) ((screen.height - size.height) / 2);
        frame.setLocation(new Point(x, y));
    }
    
    /**
     * Make your JFrame fullscreen.
     * 
     * @param frame 
     */
    public static void fullscreen(JFrame frame) {
        frame.removeNotify();
        frame.setUndecorated(true);
        frame.addNotify();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) screen.width;
        int y = (int) screen.height;
        frame.setSize(x, y);
    }
    
    /**
     * Add effect to a GTextField. @TODO:Working on this.
     */
    public static void addFocusEffect(final JTextField field, byte effect) {
        final String defaultValue = field.getText();
        if (effect == BLANK_ON_TYPE) {
            field.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent ke) {
                    if (field.getText().equals(defaultValue) || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE || ke.getKeyCode() == KeyEvent.VK_DELETE) {
                        field.setForeground(Color.BLACK);
                        field.setFont(new Font(field.getFont().getFontName(), Font.PLAIN, field.getFont().getSize()));
                        field.setText("");
                    }
                }
                @Override
                public void keyPressed(KeyEvent ke) {}
                @Override
                public void keyReleased(KeyEvent ke) {
                    if (field.getText().equals("")) {
                        field.setForeground(Color.GRAY);
                        field.setFont(new Font(field.getFont().getFontName(), Font.PLAIN, field.getFont().getSize()));
                        field.setText(defaultValue);
                    }
                }
            });
        } else if (effect == BLANK_ON_FOCUS) {
            field.addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent fe) {
                    if(field.getText().equals(defaultValue)) {
                        field.setText("");
                        field.setForeground(Color.BLACK);
                        field.setFont(new Font(field.getFont().getFontName(), Font.PLAIN, field.getFont().getSize()));
                    }
                }
                @Override
                public void focusLost(FocusEvent fe) {
                    if(field.getText().equals("")) {
                        field.setForeground(Color.GRAY);
                        field.setFont(new Font(field.getFont().getFontName(), Font.PLAIN, field.getFont().getSize()));
                        field.setText(defaultValue);
                    }
                }
            });
        }
    }
    
}